//
//  ZFHomeViewController.m
//  HappyLoan
//
//  Created by zainishenbian on 2017/3/16.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFHomeViewController.h"
#import "ZFBaseViewController1.h"//基本信息VC1
#import "ZFBaseInfoViewController2.h"//社会信息VC
#import "ZFAuthorizeInfoViewController.h"//授权信息VC
#import "ZFBindCardViewController.h"//绑定银行卡
#import "ZFFaceInfoViewController.h"//人脸识别VC
#import "ZFDQKViewController.h"//贷钱快VC
#import "ZFLoginHttpRequest.h"
#import "ZFHomeHttpRequest.h"
#import "ScrollImage.h"
#import "ZFHeader.h"

#import "ZLCBannerInfoModel.h"
#import "LZBInifiteScrollView.h"
#import "FirstBannerViewController.h"
#import "SecondBannerViewController.h"
#import "ZLCLinkUrlViewController.h"
#import "UIImageView+WebCache.h"
#import "PublicClass.h"

#define SELECTIMGMAME  @"1"
#define UNCHECKIMGNAME @"2"

@interface ZFHomeViewController ()<LZBInifiteScrollViewDelegate,ScrollImageDelegate>
{
//    NSInteger borrowTimeLimit;//借款期限
    
    NSInteger lowestTime;//最少时间
    NSInteger highestTime;//最高时间
    
    NSInteger lowestLimit;//最低额度
    NSInteger highestLimit;//最高额度
    
    NSString *selectedMoney;
    NSString *selectedTime;
    
    NSString *activityLink;
    NSString *activityTitle;
}
@property (nonatomic,strong)ZLCBannerInfoModel *BannerInfoData;
//@property (nonatomic, strong) LZBInifiteScrollView *bannerSrollView;
@property (nonatomic,strong)NSMutableArray* banneriImageDataArr;
@property (nonatomic,strong)NSMutableArray *bannerArr;
@property (nonatomic,strong)NSMutableArray *bannerNSMuArr;
@property (nonatomic,strong) AlertBoxView *alertV;

@property (nonatomic,strong) UIView *indexView;
@property (nonatomic,strong)NSMutableArray *activityArr;
@property (nonatomic,strong)UIImageView *indexButton;
@end

@implementation ZFHomeViewController
- (NSMutableArray *)bannerArr{
    if (!_bannerArr) {
        _bannerArr = [[NSMutableArray alloc] init];
    }
    return _bannerArr;
}
- (NSMutableArray *)banneriImageDataArr{
    
    if (!_banneriImageDataArr) {
        _banneriImageDataArr = [[NSMutableArray alloc] init];
    }
    return _banneriImageDataArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _indexView.hidden = YES;
    [MobClick beginLogPageView:@"homeView"];
    self.navigationController.navigationBarHidden = YES;
    [self loadFinfActivity];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"homeView"];
}
- (void)loadFinfActivity{
    
    [ZFHomeHttpRequest requestFindOneActivitySucessBlock:^(NSDictionary *dict) {
        NSLog(@"获取一个活动：HHHHHHH%@",dict);
        
        
        if ([dict[@"is_alert"] integerValue] == 1) {
            [_alertV appearAndDisappearAlertWithString:[NSString stringWithFormat:@"%@",dict[@"msg"]]];
        }
        if ([dict[@"code"] integerValue] == 1) {
            
            if ([dict[@"data"] isKindOfClass:[NSNull class]] || dict[@"data"] == nil) {
                _indexView.hidden = YES;
            }else{
            _indexView.hidden = NO;
            NSDictionary *dic = dict[@"data"];
            activityLink = dic[@"activity_link"];
            activityTitle = dic[@"activity_title"];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",dic[@"activity_pic"]]];
                [_indexButton sd_setImageWithURL:url];
            }

            
        }
        
    } failureBlock:^(NSInteger failSign) {
        
        //        [_alertBox appearAndDisappearAlertWithString:@"网络连接失败"];
    }];
    
}
- (void)enterLoginVC
{
    //第一次进入App,引导
    if ([ZFLoginConfig judgeIsFirstEnterApp] == YES) {
        
        ZFLoginViewController *loginVC = [ZFLoginConfig getLoginVC1];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self enterLoginVC];
    [self createBannerView];
    [_BorrowingBut setShadowWidth:2 shadowHeight:2 shadowColor:Main_Color1 radius:10];
    //banner数据请求
    _bannerNSMuArr = [NSMutableArray array];
    [ZFHomeHttpRequest requestBannerInfoComplete:^(NSDictionary *dict) {
        
        if ([dict[@"is_alert"] integerValue] == 1) {
            
            [self showAlertView:dict[@"msg"]];
        }
        
        if ([dict[@"code"] integerValue] == 1) {
            
            for (NSDictionary *dic in dict[@"data"][@"bannerList"]) {
                
                ZLCBannerInfoModel *modal = [ZLCBannerInfoModel getMyBannerInfoDictionary:dic];
                [_bannerNSMuArr addObject:modal];
                [self.banneriImageDataArr addObject:modal.banner_url];
            }
            [self createBannerView];
        }
    } failureBlock:^(NSInteger failSign) {
        
    }];
    
    [self initData];
    [self aboutIndexVC];
}
- (void)aboutIndexVC{
    _indexView = [[UIView alloc] init];
    _indexView.frame = CGRectMake(0, 0, MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT);
    _indexView.backgroundColor = [UIColor whiteColor];
    _indexView.alpha = 0.8;
    [self.view addSubview:_indexView];
    
    _indexButton = [[UIImageView alloc]init];
    _indexButton.frame  = CGRectMake(0, 0, ADAPTATION_WIDTH(300), ADAPTATION_HEIGHT(365));
    _indexButton.center = CGPointMake(MAINSCREEN_WIDTH/2, MAINSCREEN_HEIGHT/2+ADAPTATION_HEIGHT(40));
    _indexButton.backgroundColor = [UIColor whiteColor];
    _indexButton.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapIamge = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toindexImg:)];
    [_indexButton addGestureRecognizer:tapIamge];
    
    [_indexView addSubview:_indexButton];
    
    UIButton *ShutDownBut = [UIButton buttonWithType:UIButtonTypeCustom];
    ShutDownBut.frame = CGRectMake(ADAPTATION_WIDTH(298), ADAPTATION_HEIGHT(112), ADAPTATION_WIDTH(40), ADAPTATION_HEIGHT(85));
    ShutDownBut.backgroundColor = [UIColor redColor];
    [ShutDownBut addTarget:self action:@selector(shutDownView) forControlEvents:UIControlEventTouchUpInside];
    [_indexView addSubview:ShutDownBut];
    
}

//初始化数据
- (void)initData
{
    //禁止侧滑
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];

    //额度
    lowestLimit = 200;
    highestLimit = 10000;
    _selectLimit.text = [NSString stringWithFormat:@"￥%.2f",(CGFloat)highestLimit];
    
    _slider.minimumValue = lowestLimit/100;
    _slider.maximumValue = highestLimit/100;
    _slider.value = _slider.maximumValue;
    
    //期限
    lowestTime = 1;
    highestTime = 36;
    _timeSlider.minimumValue = lowestTime;
    _timeSlider.maximumValue = highestTime;
    _timeSlider.value = highestTime;
    
    [_slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];
    [_timeSlider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];
    
    selectedMoney = [NSString stringWithFormat:@"%ld",(long)highestLimit];
    selectedTime = [NSString stringWithFormat:@"%ld",(long)highestTime];
}
#pragma mark ———— 创建banner
- (void)createBannerView
{
    ScrollImage *scrollView = [[ScrollImage alloc] initWithCurrentController:self urlString:_banneriImageDataArr viewFrame:CGRectMake(0, 0, SCREEN_WIDTH, _BannerView.bounds.size.height) placeholderImage:[UIImage imageNamed:@"banner_waiting"]];
    scrollView.delegate = self;
    scrollView.timeInterval = 4.0;

    [self.BannerView addSubview:scrollView.view];
}
#pragma mark ———— 点击了几张图片
- (void)scrollImage:(ScrollImage *)scrollImage clickedAtIndex:(NSInteger)index
{
    if (_bannerNSMuArr.count == 0) {
        
        return;
    }
    ZLCBannerInfoModel *bannerModal = nil;
    if (index == 3001 || index == 3002) {
        
        bannerModal = _bannerNSMuArr[0];
    }
    else
    {
        bannerModal = _bannerNSMuArr[index-3002];
    }
    ZLCLinkUrlViewController * secondVC = [[ZLCLinkUrlViewController alloc]init];
    secondVC.url = bannerModal.banner_link;
    secondVC.titleStr = bannerModal.banner_name;
    secondVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:secondVC animated:YES];

}

#pragma mark ———— slider滑动事件
- (void)sliderClick:(UISlider *)slider
{
    NSInteger value = slider.value;
    
    if (slider.tag == 1) {
        
        _selectLimit.text = [NSString stringWithFormat:@"￥%.2f",(CGFloat)value*100];
        selectedMoney = [NSString stringWithFormat:@"%.2f",(CGFloat)value*100];
    }
    if (slider.tag == 2) {
        
        _selectedTimeLimit.text = [NSString stringWithFormat:@"%ld个月",(long)value];
        selectedTime = [NSString stringWithFormat:@"%ld",(long)value];
    }
}
#pragma mark ———— button的点击事件
//立即借款
- (IBAction)instentBorrow:(id)sender {
    
    if (![ZFLoginConfig isLogined]) {
        
        UIViewController *loginVC = [ZFLoginConfig getLoginVC2];
        [self presentViewController:loginVC animated:YES completion:nil];
        return;
    }
    
    UIViewController *VC = nil;

//    switch ([ZFPulicAPI getsaveInfoProgress]) {
//        case 1:
//            {
//                VC = [ZFBaseViewController1 new];
//            }
//            break;
//        case 2:
//            {
//                VC = [ZFBaseInfoViewController2 new];
//            }
//            break;
//        case 3:
//            {
//                VC = [ZFAuthorizeInfoViewController new];
//            }
//            break;
//        case 4:
//            {
//                VC = [ZFBindCardViewController new];
//            }
//            break;
//        case 5:
//            {
                VC = [ZFFaceInfoViewController new];
//            }
//            break;
//        case 6:
//            {
//                if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"phone"] isEqualToString:@"18725835901"]) {
//                    
//                    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的认证资料提交成功，我司审核需要3-7天，请您耐心等待！" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我知道了", nil];
//                    alertV.tag = 1;
//                    [alertV show];
//                }
//                else
//                {
//                    [ZFHomeHttpRequest requestDQKListInfoApplyamt:selectedMoney loanTerm:selectedTime Complete:^(NSDictionary *dict) {
//                        
//                        if ([dict[@"code"] integerValue] == 1) {
//                            
//                            ZFDQKViewController *DQKVC = [ZFDQKViewController new];
//                            DQKVC.DQKUrl = dict[@"data"][@"configValue"];
//                            DQKVC.hidesBottomBarWhenPushed = YES;
//                            [self.navigationController pushViewController:DQKVC animated:YES];
//                        }
//                    } failureBlock:^(NSInteger failSign) {
//                        
//                        [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
//                    }];
//                }
//            }
//            break;
// 
//
//        default:
//            break;
//    }
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)toindexImg:(UITapGestureRecognizer *)tap{
    ZLCLinkUrlViewController *IndexVC =  [[ZLCLinkUrlViewController alloc] init];
    IndexVC.title = activityTitle;
    IndexVC.url = activityLink;
    [self.navigationController pushViewController:IndexVC animated:YES];
    //    NSLog(@"哎呀，你点我干嘛");
}
- (void)shutDownView{
    _indexView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
